const {
  getdbcd,
  userslist,
  t_drivercreate,
  login,
  getCheck
} = require('../../utils/api')
const {
  baseUrl
} = require('../../nev').prod
Page({
  data: {
    fileList: [],
    f_name: '',
    f_phone: '',
    f_ids: '',
    userInfo: '',
    userArr: null,
    carTypeList: null,
    f_id1: [],
    password:'',
    username:''
  },
  onShow() {
    this.getchedui()
    this.userslists()
  },
  onLoad(options) {
    console.log('跳转id', JSON.parse(options.userInfo))

    this.setData({
      userInfo: JSON.parse(options.userInfo),
      password:options.padd,
      username:options.user
    })
    console.log('查看账号',options.user)
    console.log('查看密码',this.data.password)
  },
  //获取车队信息
  getchedui() {
    let data = {
      pageSize: 200,
      filter: {}
    }
    getdbcd(data).then(res => {
      
      let arr = []
      res.data.forEach(item => {
        if (item.f_name1 == '独立外部车队') {
          arr.push(item)
        }
      })
      this.setData({
        carTypeList: arr[0] // 使用转换后的数据
      })
      console.log('获取车队信息', this.data.carTypeList)
    })
  },
  //获取系统账号绑定
  userslists() {
    let data = {
      pageSize: 200,
      filter: {}
    }
    userslist(data).then(res => {
      console.log('系统账号绑定', res)
      let arr = []
      res.data.forEach(item => {
        if (item.id == this.data.userInfo.id) {
          arr.push(item)
        }
      })
      this.setData({
        userArr: arr[0] // 使用转换后的数据
      }) 
      console.log('获取全是事实', this.data.userArr)
    })
  },

  // 输入事件处理
  onNameChange(e) {
    this.setData({
      f_name: e.detail
    });
  },

  onMobileChange(e) {
    this.setData({
      f_phone: e.detail
    });
  },

  onIdCardChange(e) {
    this.setData({
      f_ids: e.detail
    });
  },

  // 表单提交
  submitForm() {
    console.log('123', this.data.formData)
    const data = {
      f_3wetj8s797x: this.data.userArr,
      f_car_team: this.data.carTypeList,
      f_id1: this.data.f_id1,
      f_ids: this.data.f_ids,
      f_name: this.data.f_name,
      f_phone: this.data.f_phone
    }
    console.log('查看数据', data)
    t_drivercreate(data).then(res=>{
      this.loginsj()
    })
    if (!this.validateForm()) return;

    // wx.navigateTo({
    //   url: '/pages/driver-complete/driver-complete'
    // });
  },
  loginsj(){
    let data = {
      account: this.data.username,
      password: this.data.password
    }
    console.log(data)
    login(data).then(res => {
      console.log(res)
      // wx.setStorageSync('userInfoData', res.data);
      if (res.errors) {
        console.log('degnlushibai')
        return
      } else {
        wx.setStorage({
          key: 'userInfoData',
          data: res.data
        });
        getCheck().then(res => {
          wx.setStorage({
            key: 'quanxian',
            data: 'siji'
          });
          wx.reLaunch({
            url: '/pages/index/index'
          })
        })
        wx.hideLoading()
      }

    }).catch(err => {
      console.log(err)
      wx.hideLoading();
      wx.showToast({
        title: '登录失败',
        icon: 'none'
      });
    });
  },
  // 验证逻辑
  validateForm() {
    const {
      f_name,
      f_phone,
      f_ids
    } = this.data;
    if (!f_name) return this.showToast('姓名不能为空');
    if (!/^1[3-9]\d{9}$/.test(f_phone)) return this.showToast('手机号格式错误');
    if (!/(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(f_ids)) return this.showToast('身份证号不合法');
    return true;
  },
  // 文件上传回调（核心方法）
  afterRead(event) {
    const that = this
    const {
      file
    } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: `${baseUrl}/attachments:create`,
      filePath: file.url,
      name: 'file',
      formData: {
        user: 'test'
      },
      header: {
        'Content-Type': 'multipart/form-data', // 必须添加
        'Authorization': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjQsInJvbGVOYW1lIjoiYWRtaW4iLCJpYXQiOjE3NDQxODgwODcsImV4cCI6MzMzMDE3ODgwODd9.xlliGiN4aDjTnnGbdrYwS257EpDFHiDHeJ2Z6C3MGRE', // 按需添加
        'x-app': 'a_1z41svq3kx6'
      },
      success(res) {
        console.log('fanhui', JSON.parse(res.data).data)
        const {
          fileList = []
        } = that.data;
        fileList.push({
          ...file,
          url: res.data
        });
        that.setData({
          fileList
        });
        that.setData({
          f_id1: [...(that.data.f_id1?.f_id1 || []), JSON.parse(res.data).data]
        })
      },
    });
  },

  showToast(msg) {
    wx.showToast({
      title: msg,
      icon: 'none'
    });
    return false;
  }
})